package com.canbot.u05.activity.customvoice

import android.app.Dialog
import android.content.Intent
import android.os.Bundle
import android.support.v7.widget.LinearLayoutManager
import android.support.v7.widget.RecyclerView
import android.view.View
import com.alibaba.fastjson.JSON
import com.canbot.u05.R
import com.canbot.u05.activity.BaseActivityNoTitle
import com.canbot.u05.activity.customvoice.adapter.CustomDialogAddQuestionAdapter
import com.canbot.u05.activity.customvoice.bean.CustomDialogExcelDatas
import com.canbot.u05.activity.customvoice.bean.QuestionBean
import com.canbot.u05.activity.customvoice.tool.CheckStringUtils
import com.canbot.u05.activity.customvoice.tool.CustomDialogView
import com.canbot.u05.activity.loge
import com.canbot.u05.activity.overridePendingTransitionExt
import com.canbot.u05.activity.showToast
import com.canbot.u05.socket.MsgSendUtils
import com.canbot.u05.socket.MsgType
import com.canbot.u05.utils.PathConst
import kotlinx.android.synthetic.main.activity_custom_dialog_add_question.*
import java.util.ArrayList

/**
 * CustomDialogAddQuestionActivity
 *
 * @author  Liu.Heng
 * @date  2019/5/29
 */
class CustomDialogAddQuestionActivity : BaseActivityNoTitle() {

    private lateinit var customDialogAddQuestionAdapter: CustomDialogAddQuestionAdapter

    private lateinit var mQuestionBeanList: MutableList<QuestionBean>

    private lateinit var customDialogExcelDatas: CustomDialogExcelDatas

    private lateinit var questionList: MutableList<String>

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_custom_dialog_add_question)
        initView()
    }

    override fun onResume() {
        super.onResume()
        MsgSendUtils.sendStringMsg(MsgType.PLAY_SOUND, PathConst.CUSTOM_DIALOG_PAYH + "u05_dzwt_001.wav")
        MsgSendUtils.sendStringMsg(MsgType.EYE_MOTION, "12")
    }

    fun initView() {
        mQuestionBeanList = ArrayList()
        val intent = intent
        customDialogExcelDatas = JSON.parseObject(intent.getStringExtra("data"), CustomDialogExcelDatas::class.java)
        questionList = JSON.parseArray(intent.getStringExtra("questions"), String::class.java)
        mQuestionBeanList = customDialogExcelDatas.listQsBean
        if (mQuestionBeanList.isNotEmpty()) {
            my_next.visibility = View.VISIBLE
        }
        question_recyclerView.layoutManager = LinearLayoutManager(this, RecyclerView.VERTICAL, false)
        customDialogAddQuestionAdapter = CustomDialogAddQuestionAdapter(this, mQuestionBeanList,
                object : CustomDialogAddQuestionAdapter.SaveEditListener {

                    override fun saveEdit(position: Int, string: String, boolean: Boolean) {
                        mQuestionBeanList[position].question = string
                        if (boolean) {
                            for (questionBean in mQuestionBeanList) {
//                                && CheckStringUtils.isChineseOrEnglish(questionBean.question)
                                if (questionBean.question.isNotEmpty() && questionBean.question.length > 1) {
                                    my_next.visibility = View.VISIBLE
                                    break
                                } else {
                                    my_next.visibility = View.GONE
                                }
                            }
                        }
                    }

                    override fun removeData(position: Int) {
                        mQuestionBeanList.removeAt(position)
                        customDialogAddQuestionAdapter.removeItem(position)
                        if (mQuestionBeanList.isEmpty()) {
                            my_next.visibility = View.GONE
                            red_text.text = ""
                        }
                    }

                })
        question_recyclerView.adapter = customDialogAddQuestionAdapter

        my_back.setOnClickListener(::onClick)
        add_question.setOnClickListener(::onClick)
        my_next.setOnClickListener(::onClick)
    }

    private fun onClick(view: View) {
        when (view.id) {
            /**返回*/
            R.id.my_back -> {
                if (customDialogExcelDatas.listAsBean.isNotEmpty()) {
                    finish()
                    overridePendingTransitionExt()
                } else {
                    CustomDialogView(this, object : CustomDialogView.OnCloseListener {
                        override fun onClick(dialog: Dialog?, confirm: Boolean) {
                            if (confirm) {
                                dialog?.dismiss()
                                finish()
                                overridePendingTransitionExt()
                            }
                        }
                    }, "退出编辑", "退出后将终止对话定制, 确认退出吗?").show()
                }
            }
            /**添加回答*/
            R.id.add_question -> {
                if (mQuestionBeanList.size > 9) {
                    showToast("问题数量已达上限了")
                    return
                }
                mQuestionBeanList.add(QuestionBean())
                customDialogAddQuestionAdapter.notifyItemInserted(mQuestionBeanList.size)
                question_recyclerView.smoothScrollToPosition(mQuestionBeanList.size - 1)
            }
            /**下一步*/
            R.id.my_next -> {
                val mSaveList: MutableList<QuestionBean> = ArrayList()
                /**删除空的问题*/
                mQuestionBeanList.forEach {
//                    && CheckStringUtils.isChineseOrEnglish(it.question)
                    if (it.question.isNotEmpty()) {
                        mSaveList.add(it)
                        loge("question=${it.question}")
                    }
                }

                /**删除相同的问题*/
                val newArr = ArrayList<QuestionBean>()

                var flag: Boolean
                mSaveList.forEach {
                    flag = false
                    for (j in newArr.indices) {
                        if (it.question == newArr[j].question) {
                            flag = true
                            break
                        }
                    }
                    if (!flag) {
                        newArr.add(it)
                    }
                }

                /**删除与数据库相同的问题*/
                questionList.forEach {

                    newArr.find { questionBean ->
                        questionBean.question == it
                    }?.let { questionBean1 ->
                        newArr.remove(questionBean1)
                    }
                }

                customDialogExcelDatas.listQsBean = newArr

                loge("newArr=$newArr")

                if (newArr.isEmpty()) {
                    showToast("问题类型出现错误")
                } else {
                    val mIntent = Intent(this, CustomDialogAddAnswerActivity::class.java)
                    mIntent.putExtra("data", JSON.toJSONString(customDialogExcelDatas))
                    startActivity(mIntent)
                    overridePendingTransitionExt()
                }
            }
            else -> {
            }
        }
    }
}
